<template>
  <el-row :gutter="10" type="flex" justify="space-between" style="width: 100%;">
    <el-col :span="6">
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="6">
      <el-row type="flex">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown @command="handelClick">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">添加部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'

export default {
  name: 'TreeTool',
  props: {
    treeNode: Object,
    isRoot: Boolean
  },
  methods: {
    async handelClick(type) {
      console.log(type)
      if (type === 'add') {
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        this.$emit('editDepts', this.treeNode)
      } else if (type === 'del') {
        // 删除操作
        await this.$confirm('确定要删除部门嘛？')
        await delDepartments(this.treeNode.id)
        this.$emit('delDepts') // 触发自定义事件
        this.$message.success('删除部门成功')
      }
    }
  }
}
</script>

<style scoped>

</style>
